<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 红灯3秒亮一次， 绿灯2秒亮一次， 黄灯1秒亮一次； 如何使用Promise让三个灯不断交替重复亮灯？

        // 具体要求如下:
        //     先等3秒以后亮红灯, 再等2秒亮绿灯, 接着继续等1秒亮黄灯,
        //     再等3秒以后亮红灯, 再等2秒亮绿灯, 接着继续等1秒亮黄灯,
        //     ...以此类推, 如何使用Promise让三个灯不断交替重复亮灯？

        {
            let timeout = 0;

            function fn() {
                let date = new Date();
                return new Promise((resolve, reject) => {
                    if (timeout == 3) {
                        resolve(["red", date]);
                    } else if (timeout == 5) {
                        resolve(["green", date]);
                    } else if (timeout == 6) {
                        timeout = 0;
                        resolve(["yellow", date]);
                    } else if (timeout > 6) {
                        reject("出错了，请刷新页面!")
                    }
                })
            }

            setInterval(async function fn1() {
                try {
                    timeout++;
                    let item = await fn();
                    console.log(item[0], '\n','\n', item[1]);
                    let body = document.querySelector("body");
                    body.style.backgroundColor = item[0];
                } catch (err) {
                    console.log(err);
                }
            }, 1000);
        }
    </script>
</body>

</html>